import common from '../vendor/common'
import '../scss/login_password.scss'
import { findPassword, getCode } from '../vendor/api'
try{
const mui = common.mui
var data = {} // 定义参数
var password = null //密码
var tel = null //电话号码
var smsCode = null //验证码
mui.init({
    swipeBack:true //启用右滑关闭功能
});
mui.plusReady(function(){
    // 初始化数据
    window.addEventListener('password', function(e){
        data = {}
        password = null
        tel = null
        smsCode = null
        mui('#password')[0].value = null
        mui('#tel')[0].value = null
        mui('#sms')[0].value = null
    })
// 确认
mui('.loginBtn').on('tap','button',(e)=>{
    // 获取input框里的值
    password = mui('#password')[0].value
    tel = mui('#tel')[0].value
    smsCode = mui('#sms')[0].value
    data.tel = tel
    data.smsCode = smsCode
    data.password = password
    // 隐藏键盘
    mui('#password')[0].blur()
    mui('#tel')[0].blur()
    mui('#sms')[0].blur()
    // 验证
    var telRpx = /^\+?[1-9][0-9]*$/
    if (!telRpx.test(tel) || tel.length !== 11) {
        mui.alert('请输入11位手机号码');
        return
    }
    if(!telRpx.test(smsCode)  || smsCode.length !== 4){
        mui.alert('请输入4位验证码')
        return
    }
    if(!password || password.trim() == ''){
        mui.alert('请输入密码');
        return
    }
    if(password.length < 6){
        mui.alert('密码最小长度为6位')
        return
    }
    if(password.length > 18){
         mui.alert('密码最大长度为18位')
         return
    } 
    findPassword(data).then((res)=>{
        if(res.data){
            mui('#password')[0].value = ''
            mui('#tel')[0].value = ''
            mui('#sms')[0].value = ''
            mui.alert(res.message)
            var ws=plus.webview.currentWebview()
            var ws1 = plus.webview.currentWebview().opener() 
            plus.webview.close(ws) // 跳转页面时关闭当前页面
            plus.webview.close(ws1) // 跳转页面时关闭父页面
            mui.openWindow({
                url: 'login_new.html',
                id: 'login_new.html'
            })
        }
    })
})
// 获取验证码
mui('#getSms')[0].addEventListener('tap',function login(e){
    // 获取手机号码输入框里的值
    var tel = mui('#tel')[0].value
    // 拼接参数
    var data = tel +'/'+'retrieve_password'
    // 手机号码不为空时可获取验证码
    if(tel !== ''){
        getCode(data).then(function(res){
            // console.log('asfsdgsdgsdf',JSON.stringify(res))
            if(res.success){
                var time = 120; //定义验证码失效的时间
                mui('#getSms')[0].classList.add('mui-hidden') //隐藏获取验证码
                mui('#time')[0].classList.remove('mui-hidden') //显示倒计时
                mui('#time')[0].innerHTML = time + 's'
                var outTime = setInterval(() => {
                    time --;
                    if(time <= 0){
                        mui('#getSms')[0].classList.remove('mui-hidden')
                        mui('#time')[0].classList.add('mui-hidden')
                        mui('#getSms')[0].innerHTML = '重新获取验证码'
                        clearInterval(outTime);
                        return;
                    }
                    mui('#time')[0].innerHTML = time + 's'
                },1000);
            }
        })
    } else {
        mui.toast('请输入手机号')
    }
})
})

}catch(e){
    console.log('login_new出错了')
}